package com.connector.server.util;

import org.slf4j.LoggerFactory;
import java.util.Iterator;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.TypeReference;
import java.util.Map;
import com.google.common.collect.Maps;
import com.connector.base.connection.channel.SocketIoSocketChannel;
import java.util.List;
import com.google.common.collect.Lists;
import com.connector.base.utils.Utils;
import cn.hutool.core.date.DateUtil;
import java.util.Date;
import com.alibaba.fastjson.JSONObject;
import com.connector.base.message.model.PushMessage;
import com.alibaba.fastjson.JSON;
import com.connector.server.jmx.JmxMessageBean;
import com.connector.base.connection.Connection;
import com.connector.base.connection.channel.SocketChannel;
import java.util.concurrent.ConcurrentMap;
import org.slf4j.Logger;

public class PushMsgUtilThread implements Runnable
{
    private static final Logger log;
    private String userId;
    private String msgType;
    private ConcurrentMap<SocketChannel, Connection> cons;
    JmxMessageBean jmxMessageBean;
    private String msg;
    private String appId;
    private Connection connection;

    public PushMsgUtilThread(final String msgType, final String userId, final JmxMessageBean jmxMessageBean, final ConcurrentMap<SocketChannel, Connection> cons) {
        this.userId = userId;
        this.msgType = msgType;
        this.cons = cons;
        this.jmxMessageBean = jmxMessageBean;
    }

    public PushMsgUtilThread(final String msgType, final String userId, final JmxMessageBean jmxMessageBean, final ConcurrentMap<SocketChannel, Connection> cons, final String msg) {
        this.userId = userId;
        this.msgType = msgType;
        this.cons = cons;
        this.msg = msg;
    }

    public PushMsgUtilThread(final Connection connection, final String msg, final String msgType) {
        this.msgType = msgType;
        this.msg = msg;
        this.connection = connection;
    }

    @Override
    public void run() {
        if (this.msgType.equals("1")) {
            this.pushMsg(this.jmxMessageBean, this.cons);
        }
        if (this.msgType.equals("2")) {
            this.pushHandshakeMsg(this.userId, this.cons);
        }
        if (this.msgType.equals("3")) {
            this.sendCertMsg(this.connection, this.msg);
        }
        if (this.msgType.equals("4")) {
            this.sendOtherMsg(this.userId, this.cons, this.msg);
        }
        if (this.msgType.equals("5")) {
            this.sendAppCertMsg(this.connection, this.msg);
        }
    }

    public void pushMsg(final JmxMessageBean messageBean, final ConcurrentMap<SocketChannel, Connection> cons) {
        PushMsgUtilThread.log.info("开始处理消息:" + JSON.toJSONString((Object)messageBean));
        if (null == messageBean) {
            PushMsgUtilThread.log.error("消息体内容是空的,不发送.");
            return;
        }
        final String sendType = messageBean.getSendType();
        if (sendType.equals("2") || sendType.equals("4")) {
            final PushMessage pushMessage = new PushMessage();
            try {
                final JSONObject jsonObject = JSONObject.parseObject(messageBean.getText().toString());
                jsonObject.put("time", (Object)Utils.formatDateTime(DateUtil.formatDateTime(new Date())));
                jsonObject.put("createTime", (Object)DateUtil.formatDateTime(new Date()));
                pushMessage.setData(jsonObject.toJSONString());
            }
            catch (Exception e) {
                pushMessage.setData(messageBean.getText());
            }
            pushMessage.setType(sendType);
            final List<String> targetIds =  Lists.newArrayList(messageBean.getTargetId().split(","));
            String msgId = null;
            if (null != messageBean.getDialogId()) {
                msgId = String.valueOf((long)messageBean.getMsgId());
            }
            final String msgJson = JSON.toJSONString((Object)pushMessage);
            PushUtil.pushMsg(targetIds, msgId, sendType, PushMessage.buildPacket(msgJson), cons, msgJson, messageBean.getMust(), messageBean.getOnlyMobile());
        }
    }

    private void pushHandshakeMsg(final String userId, final ConcurrentMap<SocketChannel, Connection> cons) {
        sendNoReadMsg(userId, cons);
    }

    private void sendCertMsg(final Connection connection, final String msg) {
        try {
            final String data = HttpUtil.transgerWithLogin();
            PushMsgUtilThread.log.info("请求版权信息结果:{}", (Object)data);
            final JSONObject dataMsg = JSON.parseObject(data);
            if (null != dataMsg.get((Object)"success") && (boolean)dataMsg.get((Object)"success")) {
                final String obj = JSONObject.toJSONString(dataMsg.get((Object)"obj"));
                PushMsgUtilThread.log.info(connection.toString());
                final SocketIoSocketChannel socketIoSocketChannel = (SocketIoSocketChannel)connection.getSocketClient();
                PushMsgUtilThread.log.info("sessionId=" + socketIoSocketChannel.getSocketIOClient().getSessionId().toString());
                PushUtil.sendCertMsg(connection, msg + "(" + obj + ")");
            }
            else {
                PushMsgUtilThread.log.error("获取版权信息出现问题:{}", (Object)data);
            }
        }
        catch (Exception e) {
            PushMsgUtilThread.log.error("获取版权信息出现问题", (Throwable)e);
        }
    }

    private void sendAppCertMsg(final Connection connection, final String msg) {
        try {
            final String data = HttpUtil.transgerWithLoginApp(connection.getSessionContext().getAppid(), msg);
            PushMsgUtilThread.log.info("请求APP版权信息结果:{}", (Object)data);
            final JSONObject dataMsg = JSON.parseObject(data);
            final String obj = JSONObject.toJSONString(dataMsg.get((Object)"obj"));
            PushMsgUtilThread.log.info(connection.toString());
            PushUtil.sendCertMsg(connection, msg + "('" + obj + "')");
        }
        catch (Exception e) {
            PushUtil.sendCertMsg(connection, msg + "('获取版权信息出现问题')");
            PushMsgUtilThread.log.error("获取版权信息出现问题", (Throwable)e);
        }
    }

    private void sendOtherMsg(final String userId, final ConcurrentMap<SocketChannel, Connection> cons, final String msg) {
        try {
            PushUtil.sendOtherMsg(userId, msg, cons);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void sendNoReadMsg(final String userId, final ConcurrentMap<SocketChannel, Connection> cons) {
        try {
            final Map<String, String> paramMap =  Maps.newHashMap();
            paramMap.put("userId", userId);
            final List<Map<String, Object>> noReadMsgList =  Lists.newArrayList();
            final String data = HttpUtil.postToInstantServer_getNoReadMsgByUser(paramMap);
            final Map<String, Object> dataMsg =  JSON.parseObject(data, Map.class);
            boolean success = (boolean)dataMsg.get("success");
            if (success) {
                final List<Map<String, Object>> result = (List<Map<String, Object>>)JSON.parseObject(JSON.toJSONString(dataMsg.get("obj")), (TypeReference)new TypeReference<List<Map<String, Object>>>() {}, new Feature[0]);
                if (null != result && result.size() > 0) {
                    for (final Map<String, Object> map : result) {
                        final Map<String, Object> d = Maps.newHashMap();
                        d.put("type", map.get("newType"));
                        d.put("contentType", map.get("contentType"));
                        d.put("data", map.get("content"));
                        try {
                            final JSONObject jsonObject = JSONObject.parseObject(map.get("content").toString());
                            jsonObject.put("time", (Object)Utils.formatDateTime(String.valueOf(map.get("createTime"))));
                            jsonObject.put("createTime", map.get("createTime"));
                            d.put("data", jsonObject.toJSONString());
                        }
                        catch (Exception ex) {}
                        noReadMsgList.add(d);
                    }
                    PushUtil.pushNoReadMsg(userId, JSON.toJSONString((Object)noReadMsgList), cons);
                }
            }
            else {
                PushMsgUtilThread.log.error("获取用户的未读消息出现问题{}", dataMsg.get("message"));
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getUserId() {
        return this.userId;
    }

    public String getMsgType() {
        return this.msgType;
    }

    public ConcurrentMap<SocketChannel, Connection> getCons() {
        return this.cons;
    }

    public JmxMessageBean getJmxMessageBean() {
        return this.jmxMessageBean;
    }

    public String getMsg() {
        return this.msg;
    }

    public String getAppId() {
        return this.appId;
    }

    public Connection getConnection() {
        return this.connection;
    }

    public void setUserId(final String userId) {
        this.userId = userId;
    }

    public void setMsgType(final String msgType) {
        this.msgType = msgType;
    }

    public void setCons(final ConcurrentMap<SocketChannel, Connection> cons) {
        this.cons = cons;
    }

    public void setJmxMessageBean(final JmxMessageBean jmxMessageBean) {
        this.jmxMessageBean = jmxMessageBean;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    public void setAppId(final String appId) {
        this.appId = appId;
    }

    public void setConnection(final Connection connection) {
        this.connection = connection;
    }

    static {
        log = LoggerFactory.getLogger((Class)PushMsgUtilThread.class);
    }
}
